﻿


using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;

using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.Model;
using Gift.DAL;
using Gift.Utile;


namespace WebApp.admin.user
{
    public partial class user : BaseAdmin
    {
        // Fields
        tbUser oUser = new tbUser();
        protected string ot = "";

        protected string registerIP = "";
        protected string registerTime = "";
    

        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                if (base.Request.QueryString["ot"] == "ed")
                {
                    oUser = BaseFactory.theUser.GetModel(int.Parse(Request.QueryString["id"]));
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.UserEdit);
                }
                else
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.UserAdd);
                }

                this.oUser.userRank = Convert.ToInt32(base.Request.Form["userRank"]);
                if (this.txtUserPwd.Value != "")
                {
                    this.oUser.userPwd = Gift.Security.DESEncrypt.Encrypt(this.txtUserPwd.Value);
                }
                this.oUser.email = this.txtEmail.Value;
                this.oUser.payPoints = Convert.ToInt32(this.txtPayPoints.Value);
                this.oUser.money = Convert.ToDecimal(this.txtMoney.Value);
                this.oUser.state = MathLibrary.ConvertNumToBool(base.Request.Form["state"]);
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oUser.id = Convert.ToInt32(base.Request.QueryString["id"]);
                    if (BaseFactory.theUser.Update(this.oUser))
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"userList.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectUserRank\",\"" + base.Request.Form["userRank"] + "\");alert(\"更新失败！\");</script>");
                    }
                }
                else
                {
                    this.oUser.userName = this.txtUserName.Value;
                    this.oUser.registerIP = new ClinetMAC().GetIPAddress();
                    if (BaseFactory.theUser.Add(this.oUser) > 0)
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"userList.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectUserRank\",\"" + base.Request.Form["userRank"] + "\");alert(\"添加失败！\");</script>");
                    }
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectUserRank\",\"" + base.Request.Form["userRank"] + "\");alert(\"更新失败！\");</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectUserRank\",\"" + base.Request.Form["userRank"] + "\");alert(\"添加失败！\");</script>");
                }
            }
        }

        protected StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null))
                {
                    builder.AppendLine("编辑会员");
                    return builder;
                }
                builder.AppendLine("添加会员");
            }
            catch
            {
            }
            return builder;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
            //{
            //    base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
            //}
            if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.UserBind();
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sRti", "$(\"#trRegisterTime\").show();$(\"#trRegisterIP\").show();", true);
                }
                else
                {
                    this.rdState1.Checked = true;
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sRti", "$(\"#trRegisterTime\").hide();$(\"#trRegisterIP\").hide();", true);
                }
            }
        }

        private void UserBind()
        {
            try
            {
                DataTable userById = BaseFactory.theUser.GetUserById(Convert.ToInt32(base.Request.QueryString["id"]));
                if (userById.Rows.Count > 0)
                {
                    this.txtUserName.Value = userById.Rows[0]["userName"].ToString();
                    this.txtUserName.Attributes.Add("readonly", "readonly");
                    this.txtEmail.Value = userById.Rows[0]["email"].ToString();
                    this.txtPayPoints.Value = userById.Rows[0]["payPoints"].ToString();
                    this.txtMoney.Value = decimal.Round(Convert.ToDecimal(userById.Rows[0]["money"]), 2).ToString();
                    base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script type=\"text/javascript\">selectIsExitItem(\"selectUserRank\",\"" + userById.Rows[0]["userRank"].ToString() + "\");</script>");
                    if (Convert.ToInt32(userById.Rows[0]["state"]) == 1)
                    {
                        this.rdState1.Checked = true;
                    }
                    else
                    {
                        this.rdState0.Checked = true;
                    }
                    this.registerTime = userById.Rows[0]["registerTime"].ToString();
                    this.registerIP = userById.Rows[0]["registerIP"].ToString();
                }
            }
            catch
            {
            }
        }

        protected StringBuilder UserRankBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable allUserRank = BaseFactory.theUserRank.GetList("").Tables[0];
                foreach (DataRow row in allUserRank.Rows)
                {
                    if (Convert.ToInt32(row["isDefault"]) == 1)
                    {
                        builder.AppendLine("<option value=\"" + row["id"].ToString() + "\" selected=\"selected\">" + row["rankName"].ToString() + "</option>");
                    }
                    else
                    {
                        builder.AppendLine("<option value=\"" + row["id"].ToString() + "\">" + row["rankName"].ToString() + "</option>");
                    }
                }
                return builder;
            }
            catch
            {
                return builder;
            }
        }

    }
}